package io.gitee.zhangbinhub.admin.log.conf

import io.gitee.zhangbinhub.acp.boot.tools.EnvironmentTools
import io.gitee.zhangbinhub.admin.common.constant.RouteConstant
import org.springframework.boot.SpringApplication
import org.springframework.boot.env.EnvironmentPostProcessor
import org.springframework.cloud.function.context.FunctionProperties
import org.springframework.core.env.ConfigurableEnvironment
import org.springframework.util.MimeTypeUtils

/**
 * @since JDK 11
 */
class RouteLogEnvironmentPostProcessor : EnvironmentPostProcessor {
    private val overridePropertiesName = "acpCloudRouteOverrideProperties"
    private val defaultPropertiesName = "acpCloudRouteDefaultProperties"
    private val functionDefinitionProperties = "${FunctionProperties.PREFIX}.definition"
    private val routeLogConsumerBindName = "${RouteConstant.ROUTE_LOG_CONSUMER}-in-0"

    override fun postProcessEnvironment(environment: ConfigurableEnvironment?, application: SpringApplication?) {
        val overrides: MutableMap<String, Any> = HashMap()
        var definition = RouteConstant.ROUTE_LOG_CONSUMER
        if (environment!!.containsProperty(functionDefinitionProperties)) {
            val property = environment.getProperty(functionDefinitionProperties)
            if (property != null && property.contains(definition)) {
                return
            }
            definition = "$property;$definition"
        }
        overrides[functionDefinitionProperties] = definition
        EnvironmentTools.addOrReplaceProperty(environment.propertySources, overrides, overridePropertiesName, true)

        val default: MutableMap<String, Any> = HashMap()
        "spring.cloud.stream".let { prefix ->
            default["$prefix.function.bindings.$routeLogConsumerBindName"] = RouteConstant.ROUTE_LOG_INPUT
            val destination = RouteConstant.ROUTE_LOG_DESCRIPTION
            default["$prefix.bindings.${RouteConstant.ROUTE_LOG_INPUT}.destination"] = destination
            default["$prefix.bindings.${RouteConstant.ROUTE_LOG_INPUT}.contentType"] =
                MimeTypeUtils.APPLICATION_JSON_VALUE
            default["$prefix.bindings.${RouteConstant.ROUTE_LOG_INPUT}.group"] = RouteConstant.ROUTE_LOG_CONSUMER_GROUP
            EnvironmentTools.addOrReplaceProperty(environment.propertySources, default, defaultPropertiesName, false)
        }
    }
}